package com.example.seckill.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.example.seckill.controller.co.UserCO;
import com.example.seckill.error.BusinessException;
import com.example.seckill.error.CommonErrorEnum;
import com.example.seckill.mappstruct.UserMap;
import com.example.seckill.response.CommonReturnType;
import com.example.seckill.service.UserInfoService;
import com.example.seckill.service.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserInfoService userInfoService;

    @GetMapping("/getid")
    public CommonReturnType getById(@RequestParam("id") Integer id) throws Exception {
        UserModel userModel = userInfoService.getUserById(id);
        return CommonReturnType.create(UserMap.INSTANCE.convertVoFromModel(userModel));
    }

    @GetMapping("/getotp")
    public CommonReturnType getotp(@RequestParam String telphone, HttpSession httpSession) {
        String otpCode = RandomUtil.randomNumbers(6);
        httpSession.setAttribute(telphone, otpCode);//todo 改成redis
        log.info("手机号:{},对应短信验证码:{}",telphone,otpCode);
        return CommonReturnType.create(otpCode);
    }

    @PostMapping("/register")
    public CommonReturnType register(@RequestBody UserCO userCO, HttpSession httpSession) {
        log.info("用户注册");
        //校验验证码
        String codeInSession = String.valueOf(httpSession.getAttribute(userCO.getTelphone()));
        if(!StrUtil.equals(codeInSession, userCO.getCode())){
            throw new BusinessException(CommonErrorEnum.PARAMETER_VALIDATION_ERROR, "验证码错误");
        }
        userInfoService.register(convertFromDTO(userCO));
        return CommonReturnType.create(null);
    }

    @PostMapping("/login")
    public CommonReturnType login(@RequestBody UserCO userCO, HttpSession httpSession) {
        log.info("登陆逻辑开始");
        //校验用户名和密码
        if(StrUtil.isBlank(userCO.getTelphone()) || StrUtil.isBlank(userCO.getPassword())){
            throw new BusinessException(CommonErrorEnum.PARAMETER_VALIDATION_ERROR,"用户名和密码不能为空");
        }
        String encryptPassword = MD5.create().digestHex(userCO.getPassword());
        //判断帐号密码正确
        UserModel userModel=userInfoService.validateLogin(userCO.getTelphone(),encryptPassword);
        //存放到session
        httpSession.setAttribute("IS_LOGIN",true);
        httpSession.setAttribute("LOGIN_USER",userModel);
        log.info("登陆成功,手机号为:{}",userModel.getTelphone());
        return CommonReturnType.create(null);
    }


    private UserModel convertFromDTO(UserCO userCO) {
        UserModel userModel = UserMap.INSTANCE.convertFromDTO(userCO);
        userModel.setEncryptPassword(MD5.create().digestHex(userCO.getPassword()));
        return userModel;

    }
}
